package _test.wrap;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;


public class FileTestClass
{

    private static class MyFile extends java.io.File
    {
        /**
		 * 
		 */
        private static final long serialVersionUID = -3099372738795921069L;


        public MyFile()
        {
            super( "/foo/bar/file2" );
        }
    }


    // regular java.io.File
    public static File normalFile( String path ) throws Exception
    {
        File f = new File( path );
        return f;
    }


    public static String askForGetPath( File f )
    {
        String path = f.getPath();
        return path;
    }


    public static String askForCanGetPath( File f ) throws Exception
    {
        return f.getCanonicalPath();
    }


    public static String askForAbsPath( File f )
    {
        return f.getAbsolutePath();
    }


    // * non-anonymous subclass
    // with name passed in constructor call

    public static File superClassCallFile() throws Exception
    {
        MyFile mf = new MyFile();
        return (File)mf;
    }


    // anonymous subclass
    public static File overloadedGetPathFile() throws Exception
    {

        File f = new File( "file3" )
        {
            /**
			 * 
			 */
            private static final long serialVersionUID = -7182982641397194819L;


            // try fooling the VM by overriding getPath()
            public String getPath()
            {
                String sp = super.getPath();
                return "/fake/mustnotbeused/" + sp;
            }
        };
        return f;
    }


    public static Boolean paramMethodOver()
    {
        File f = new File( "blah" )
        {
            /**
			 * 
			 */
            private static final long serialVersionUID = -5986078865755866122L;


            public boolean equals( Object o )
            {
                return true;
            }
        };
        return f.equals( null );
    }


    public static File testFileStringConstructor( String f, String s )
    {
        File testFile;
        if ( f == null )
        {
            testFile = null;
        }
        else
        {
            testFile = new File( f );
        }
        return new File( testFile, s );
    }


    public static File fileHeirarch1() throws Exception
    {
        File f = new File( "/parentPath/parentFile" );
        File f2 = new File( f, "/childPath/childFile" );
        return f2;
    }


    public static File fileHeirarch2() throws Exception
    {
        File f = null;
        File f2 = new File( f, "childPath/childFile" );
        return f2;
    }


    public static File stringHeirarch( String parent, String child )
        throws Exception
    {
        File f = new File( parent, child );
        return f;
    }


    public static File uriFile() throws Exception
    {

        URI testURI = new URI( "file:///foo.bar" );
        return new File( testURI );

    }


    public static File createTempFile( String prefix, String suffix )
    {
        File tempFile;
        try
        {
            tempFile = File.createTempFile( prefix, suffix );
            return tempFile;
        }
        catch ( IOException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static File createTempFile(
        String prefix,
        String suffix,
        File directory )
    {
        File tempFile;
        try
        {
            tempFile = File.createTempFile( prefix, suffix, directory );
            return tempFile;
        }
        catch ( IOException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    @SuppressWarnings("unused")
    public static File getAbsoluteFile()
    {
        File fileToGet = new File( "foo/bar" );
        File absFile = fileToGet.getAbsoluteFile();
        return fileToGet.getAbsoluteFile();
    }


    public static File getCanonicalFile()
    {
        File fileToGet = new File( "foo/bar" );

        try
        {
            return fileToGet.getCanonicalFile();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        return null;
    }


    public static File getParentFile( String fileName )
    {
        File fileToGet = new File( fileName );

        return fileToGet.getParentFile();

    }


    public static String getParent( String fileName )
    {
        File fileToGet = new File( fileName );

        return fileToGet.getParent();

    }


    public static String getToString()
    {
        File fileToGet = new File( "/foo/bar" );

        return fileToGet.toString();

    }


    public static File[] listFiles()
    {
        File directory = new File( "" );
        File[] listOfFiles = directory.listFiles();
        return listOfFiles;
    }


    public static File[] listFilesAlt1()
    {
        File directory = new File( "" );
        File[] listOfFiles = directory.listFiles( new FilenameFilter()
        {

            public boolean accept( File dir, String name )
            {
                return true;
            }

        } );
        return listOfFiles;
    }


    public static File[] listFilesAlt2()
    {
        File directory = new File( "" );
        File[] listOfFiles = directory.listFiles( new FileFilter()
        {
            public boolean accept( File pathname )
            {
                // TODO Auto-generated method stub
                return true;
            }

        } );
        return listOfFiles;
    }


    public static File[] listRoots()
    {
        return File.listRoots();
    }


    public static URI toURI()
    {
        File testFile = new File( "test" );
        return testFile.toURI();
    }


    @SuppressWarnings("deprecation")
    public static URL toURL()
    {
        File testFile = new File( "test/foo.bar" );
        try
        {
            return testFile.toURL();
        }
        catch ( MalformedURLException e )
        {
            return null;
        }
    }


    public static Integer testCompareTo()
    {
        File test1 = new File( "/foo/bar/foo.bar" );
        File test2 = new File( "foo/bar/foo.bar" );
        return test1.compareTo( test2 );
    }


    @SuppressWarnings("unused")
    public static File[] testInherit()
    {
        MyFileA fileA = new MyFileA( "/test" );
        MyFileB fileB = new MyFileB( "/test" );
        FileExtend extend = new FileExtend2( "/test" );
        return new File[] { fileA, fileB };
    }


    public static String getName( String fileName )
    {
        File f = new File( fileName );
        return f.getName();
    }


    public static Boolean isAbsolute( String fileName )
    {
        File f = new File( fileName );
        return f.isAbsolute();
    }


    public static String[] list( String fileName )
    {
        File f = new File( fileName );
        return f.list();
    }
}
